package com.heima.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.mapper.NodeMapper;
import com.heima.mapper.PartnerMapper;
import com.heima.mapper.VendingMachineMapper;
import com.heima.pojo.dto.PageQueryDTO;
import com.heima.pojo.dto.PartnerDTO;
import com.heima.pojo.entity.Partner;
import com.heima.pojo.vo.PartnerQueryVO;
import com.heima.pojo.vo.PartnerVO;
import com.heima.result.PageResult;
import com.heima.service.PartnerService;
import com.heima.utils.Md5Util;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class PartnerServiceImpl implements PartnerService {
    @Autowired
    PartnerMapper partnerMapper;
    @Autowired
    VendingMachineMapper vendingMachineMapper;
    @Autowired
    NodeMapper nodeMapper;
    /**
     * 供应商新增
     */
    @Override
    public boolean insert(PartnerDTO dto) {
        //TODO 业务逻辑未添加
        Partner partner = new Partner();
        partner.setCreateTime(LocalDateTime.now());
        partner.setUpdateTime(LocalDateTime.now());
        BeanUtils.copyProperties(dto, partner);
        String md5password = Md5Util.Md5(dto.getPassword());
        partner.setPassword(md5password);
        int result = partnerMapper.insert(partner);
        return result > 0;
    }

    /**
     * 供货商查询接口
     */
    @Override
    public PageResult<PartnerQueryVO> page(PageQueryDTO dto) {
        PageHelper.startPage(dto.getPageIndex(), dto.getPageSize());
        Page<PartnerQueryVO> page = partnerMapper.page(dto);
        return new PageResult<>(dto.getPageIndex(),dto.getPageSize(),page.getPages(),page.getTotal(),page.getResult()) ;
    }

    /**
     * 根据Id查询
     */
    @Override
    public PartnerVO selectById(Integer id) {
        PartnerVO vo = partnerMapper.selectById(id);
        return vo;
    }

    /**
     * 修改
     */
    @Override
    public boolean update(Integer id, PartnerDTO dtoa) {
        Partner partner = new Partner();
        partner.setId(id);
        BeanUtils.copyProperties(dtoa,partner);
        //TODO
        partner.setUpdateTime(LocalDateTime.now());
        int result = partnerMapper.put(partner);
        return result > 0;
    }
    /**
     * 删除供应商
     */
    @Override
    public boolean del(Integer id) {
        int result = partnerMapper.del(id);
        return result>0;
    }
}
